package cn.backpackerxl.servlet;

import cn.backpackerxl.service.StudentService;
import cn.backpackerxl.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet(name = "PageServlet", value = "/page")
public class PageServlet extends HttpServlet {
    private final StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        //获取不同的响应操作
        String action = request.getParameter("action");

        //使用反射机制执行不同的响应事件
        try {
            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    /**
     * 处理所有学生的分页请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String info = request.getParameter("info");
        if (info.equals("success")) {
            request.setAttribute("success", " 操作成功！");
        } else if (info.equals("error")) {
            request.setAttribute("error", " 操作失败！");
        }
        request.setAttribute("pageStudent", studentService.findAll(Integer.parseInt(currentPage), 8));
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * 处理搜索学生的分页请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        String currentPage = request.getParameter("currentPage");
        request.setAttribute("key", key);
        request.setAttribute("pageStudent", studentService.findByName(key, Integer.parseInt(currentPage), 8));
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * 处理删除修改的分页请求
     *
     * @param request
     * @param response
     */
    public void pageOption(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String info = request.getParameter("info");
        if (info.equals("success")) {
            request.setAttribute("success", " 操作成功！");
        } else if (info.equals("error")) {
            request.setAttribute("error", " 操作失败！");
        } else if (info.equals("warn")) {
            request.setAttribute("warn", " 操作未被执行!");
        }
        request.setAttribute("pageStudent", studentService.findAll(Integer.parseInt(currentPage), 8));
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
